﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

using ESOL.Business.Mst;
using ESOL.Business.Hr;
using ESOL.Common;
using ESOL.Common.Entity;
using ESOL.WinUI.MasterData;
using ESOL.WinUI.Hr;

namespace ESOL.WinUI.Finance
{
    public partial class frmManageMonthSalary : BaseForm.frmBaseMdiBusinessForm
    {
        #region Variable
        private HrMonthSalaryEntity mSelectedMonthSalary;
        private string mSelectedPersonId;
        #endregion

        #region Constructor
        public frmManageMonthSalary()
        {
            InitializeComponent();
        }
        #endregion

        #region Functions
        private void BindComboBox()
        {
            UICommon.UICommonFunctions.BindComboboxByCommonItem(cmbPersonStatus, "PS", true);
            UICommon.UICommonFunctions.BindComboboxByCommonItem(cmbSalaryStatus, "MSS", true); 
            
            int year = 2011;
            while (year <= DateTime.Now.Year + 2)
            {
                cmbYear.Items.Add(year);
                year++;
            }
            cmbYear.Text = DateTime.Now.Year.ToString();
            
            int month = 1;
            while (month <= 12)
            {
                cmbMonth.Items.Add(month);
                month++;
            }
            cmbMonth.Text = DateTime.Now.Month.ToString();

            UICommon.UICommonFunctions.BindGridComboboxByCommonItem(colPERSON_STATUS, "PS", true);
            UICommon.UICommonFunctions.BindGridComboboxByCommonItem(colSTATUS, "MSS", true);
            UICommon.UICommonFunctions.BindGridComboboxByCommonItem(colSALARY_ITEM, "SI", false);
        }
        private string GetCondition()
        {
            string condition = "";
            if (cmbSalaryStatus.SelectedValue != null && cmbSalaryStatus.SelectedValue.ToString().Trim() != "")
            {
                condition += " AND MS.STATUS = '" + cmbSalaryStatus.SelectedValue.ToString().Trim() + "' ";
            }
            if (cmbPersonStatus.SelectedValue != null && cmbPersonStatus.SelectedValue.ToString().Trim() != "")
            {
                condition += " AND P.STATUS = '" + cmbPersonStatus.SelectedValue.ToString().Trim() + "' ";
            }
            if (txtDepartment.Tag != null && txtDepartment.Tag.GetType() == typeof(MstDepartmentEntity))
            {
                MstDepartmentEntity entity = (MstDepartmentEntity)txtDepartment.Tag;
                condition += " AND D.DEPARTMENT_ID = '" + entity.DepartmentId + "' ";
            }
            if (txtPerson.Tag != null && txtPerson.Tag.GetType() == typeof(MstPersonEntity))
            {
                MstPersonEntity entity = (MstPersonEntity)txtPerson.Tag;
                condition += " AND P.PERSON_ID = '" + entity.PersonId + "' ";
            }
            return condition;
        }
        private string GetSalaryMonth()
        {
            char pad = Convert.ToChar("0");
            string salaryMonth = cmbYear.Text + cmbMonth.Text.PadLeft(2, pad);
            return salaryMonth;
        }
        private void BindDataGrid()
        {
            if (string.IsNullOrEmpty(cmbYear.Text) || string.IsNullOrEmpty(cmbMonth.Text))
            {
                return;
            }
            string salaryMonth = GetSalaryMonth();
            string condition = GetCondition();
            string orderBy = "PERSON_CODE";
            DataTable dt = HrMonthSalaryBs.GetByCondition(condition, salaryMonth, orderBy);
            DataColumn dc=new DataColumn("SelectedFlag",typeof(bool));
            dc.DefaultValue=false;
            dt.Columns.Add(dc);
            gvMain.DataSource = dt;
        }
        private void BindDetailGrid(string monthSalaryId)
        {
            if (string.IsNullOrEmpty(monthSalaryId))
            {
                gvDetail.DataSource = null;
                return;
            }
            DataSet ds = HrMonthSalaryItemBs.GetHrMonthSalaryItemDataSet(monthSalaryId);
            if (ds == null || ds.Tables.Count == 0)
            {
                gvDetail.DataSource = null;
            }
            gvDetail.DataSource = ds.Tables[0];
        }
        private void ResetButtonsEnable()
        {
            btnModify.Enabled = false;
            btnDelete.Enabled = false;
            btnSubmit.Enabled = false;
            //brnApprove.Enabled = false;
            btnPaySalary.Enabled = false;
            if (mSelectedMonthSalary == null)
            {
                return;
            }
            if (mSelectedMonthSalary.Status == "MSS01"
                || mSelectedMonthSalary.Status == "MSS02"
                || mSelectedMonthSalary.Status == "MSS04")
            {
                btnModify.Enabled = true;
                btnDelete.Enabled = true;
                //brnApprove.Enabled = true;
            }
            else if (mSelectedMonthSalary.Status == "MSS03")
            {
                btnPaySalary.Enabled = true;
            }
            else if (string.IsNullOrEmpty(mSelectedMonthSalary.Status))
            {
                btnModify.Enabled = true;
            }
        }
        #endregion

        #region Event
        protected override void OnShown(EventArgs e)
        {
            base.OnShown(e);
            BindComboBox();
            BindDataGrid();
        }

        DataRow drSelected;
        private void gridView1_FocusedRowChanged(object sender, DevExpress.XtraGrid.Views.Base.FocusedRowChangedEventArgs e)
        {
            drSelected = gridView1.GetDataRow(e.FocusedRowHandle);
            if (drSelected["SELECTED_PERSON"] != null)
            {
                mSelectedPersonId = drSelected["SELECTED_PERSON"].ToString();
            }
            mSelectedMonthSalary = new HrMonthSalaryEntity(drSelected);
            BindDetailGrid(mSelectedMonthSalary.MonthSalaryId);
            ResetButtonsEnable();
        }

        private void repositoryItemCheckEdit1_Click(object sender, EventArgs e)
        {
            if ((bool)drSelected["SelectedFlag"])
            {
                drSelected["SelectedFlag"] = false;
            }
            else
            {
                drSelected["SelectedFlag"] = true;
            }
        }

        protected override void Click_View()
        {
            if (mSelectedMonthSalary == null || string.IsNullOrEmpty(mSelectedPersonId))
            {
                MessageBox.Show("请选择一个员工！", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }
            MstPersonEntity person = MstPersonBs.GetMstPersonByID(mSelectedPersonId);
            string salaryMonth = GetSalaryMonth();
            frmMaintainMonthSalary frm = new frmMaintainMonthSalary(salaryMonth, person, mSelectedMonthSalary);
            frm.ThisActionType = ActionType.View;
            frm.ShowDialog();
        }

        protected override void Click_Delete()
        {
            if (mSelectedMonthSalary == null || string.IsNullOrEmpty(mSelectedPersonId))
            {
                MessageBox.Show("请选择一个员工！", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }
            DialogResult dr = MessageBox.Show("是否删除选中员工？", "Question", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
            if (dr == DialogResult.No)
            {
                return;
            }
            try
            {
                mSelectedMonthSalary.Status = "MSS10";
                mSelectedMonthSalary.LastModifiedBy = CommonParameter.CurrentUser.PersonId;
                mSelectedMonthSalary.LastModifiedDate = DateTime.Now;
                bool successful = HrMonthSalaryBs.UpdateHrMonthSalary(mSelectedMonthSalary);
                if (successful)
                {
                    MessageBox.Show("删除成功！", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    BindDataGrid();
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
        }

        protected override void Click_Submit()
        {
            if (mSelectedMonthSalary == null || string.IsNullOrEmpty(mSelectedPersonId))
            {
                MessageBox.Show("请选择一个员工！", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }
            DialogResult dr = MessageBox.Show("是否送审选中员工？", "Question", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
            if (dr == DialogResult.No)
            {
                return;
            }
            try
            {
                mSelectedMonthSalary.Status = "MSS02";
                mSelectedMonthSalary.LastModifiedBy = CommonParameter.CurrentUser.PersonId;
                mSelectedMonthSalary.LastModifiedDate = DateTime.Now;
                bool successful = HrMonthSalaryBs.UpdateHrMonthSalary(mSelectedMonthSalary);
                if (successful)
                {
                    MessageBox.Show("送审成功！", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    BindDataGrid();
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
        }

        protected override void Click_Approve()
        {
            List<HrMonthSalaryEntity> list = new List<HrMonthSalaryEntity>();
            DataTable dt = gvMain.DataSource as DataTable;
            foreach(DataRow dr in dt.Rows)
            {
                if ((bool)dr["SelectedFlag"])
                {
                    HrMonthSalaryEntity entity = new HrMonthSalaryEntity(dr);
                    if (entity.Status == "MSS02")
                    {
                        entity.Status = "MSS03";
                        entity.ApprovedDate = DateTime.Now;
                        entity.ApprovedBy = CommonParameter.CurrentUser.PersonId;
                        list.Add(entity);
                    }
                }
            }
            if (list.Count == 0)
            {
                MessageBox.Show("请至少选择一个可审核的员工！", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }
            bool result = HrMonthSalaryBs.UpdateHrMonthSalary(list);
            if (result)
            {
                BindDataGrid();
            }
        }

        protected override void Click_Modify()
        {
            if (mSelectedMonthSalary == null || string.IsNullOrEmpty(mSelectedMonthSalary.Status) || mSelectedMonthSalary.Status == "MSS01")
            {
                MstPersonEntity person = MstPersonBs.GetMstPersonByID(mSelectedPersonId);
                string salaryMonth = GetSalaryMonth();
                frmMaintainMonthSalary frm = new frmMaintainMonthSalary(salaryMonth, person, null);
                frm.ThisActionType = ActionType.New;
                DialogResult dr = frm.ShowDialog();
                if (dr == System.Windows.Forms.DialogResult.OK)
                {
                    BindDataGrid();
                }
            }
            else
            {
                if (mSelectedMonthSalary == null || string.IsNullOrEmpty(mSelectedPersonId))
                {
                    MessageBox.Show("请选择一个员工！", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    return;
                }
                MstPersonEntity person = MstPersonBs.GetMstPersonByID(mSelectedPersonId);
                string salaryMonth = GetSalaryMonth();
                frmMaintainMonthSalary frm = new frmMaintainMonthSalary(salaryMonth, person, mSelectedMonthSalary);
                frm.ThisActionType = ActionType.Modify;
                DialogResult dr = frm.ShowDialog();
                if (dr == System.Windows.Forms.DialogResult.OK)
                {
                    BindDataGrid();
                }
            }
        }

        protected override void Click_New()
        {
            MstPersonEntity person = MstPersonBs.GetMstPersonByID(mSelectedPersonId);
            string salaryMonth = GetSalaryMonth();
            frmMaintainMonthSalary frm = new frmMaintainMonthSalary(salaryMonth, person, null);
            frm.ThisActionType = ActionType.New;
            DialogResult dr = frm.ShowDialog();
            if (dr == System.Windows.Forms.DialogResult.OK)
            {
                BindDataGrid();
            }
        }

        protected override void Click_Query()
        {
            BindDataGrid();
        }

        private void txtDepartment_ButtonClick(object sender, DevExpress.XtraEditors.Controls.ButtonPressedEventArgs e)
        {
            frmMaintainDepartmentQuery frm = new frmMaintainDepartmentQuery();
            if (frm.ShowDialog() == DialogResult.OK && frm.SelectedDepartment != null)
            {
                txtDepartment.Text = frm.SelectedDepartment.DepartmentName;
                txtDepartment.Tag = frm.SelectedDepartment;
            }
            else
            {
                txtDepartment.Text = "";
                txtDepartment.Tag = null;
            }
        }

        private void txtPerson_ButtonClick(object sender, DevExpress.XtraEditors.Controls.ButtonPressedEventArgs e)
        {
            frmMaintainEmployeeQuery frm = new frmMaintainEmployeeQuery(true);
            if (frm.ShowDialog() == DialogResult.OK && frm.SelectedPerson != null)
            {
                txtPerson.Text = frm.SelectedPerson.NameCn;
                txtPerson.Tag = frm.SelectedPerson;
            }
            else
            {
                txtPerson.Text = "";
                txtPerson.Tag = null;
            }
        }

        private void btnPaySalary_Click(object sender, EventArgs e)
        {
            List<HrMonthSalaryEntity> list = new List<HrMonthSalaryEntity>();
            DataTable dt = gvMain.DataSource as DataTable;
            foreach(DataRow dr in dt.Rows)
            {
                if ((bool)dr["SelectedFlag"])
                {
                    HrMonthSalaryEntity entity = new HrMonthSalaryEntity(dr);
                    if (entity.Status == "MSS03")
                    {
                        entity.Status = "MSS05";
                        list.Add(entity);
                    }
                }
            }
            if (list.Count == 0)
            {
                MessageBox.Show("请至少选择一个可发工资的员工！", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }
            bool result = HrMonthSalaryBs.UpdateHrMonthSalary(list);
            if (result)
            {
                BindDataGrid();
            }
        }

        #endregion

    }
}
